package com.indexingsystem.system.service.impl;
//package com.indexingsystem.system.service.impl;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Set;
//
//import org.apache.commons.lang.StringUtils;
//import org.quartz.CronScheduleBuilder;
//import org.quartz.CronTrigger;
//import org.quartz.Job;
//import org.quartz.JobBuilder;
//import org.quartz.JobDetail;
//import org.quartz.JobExecutionContext;
//import org.quartz.JobKey;
//import org.quartz.Scheduler;
//import org.quartz.SchedulerException;
//import org.quartz.Trigger;
//import org.quartz.TriggerBuilder;
//import org.quartz.TriggerKey;
//import org.quartz.impl.matchers.GroupMatcher;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import com.indexingsystem.boss.entity.Tasks;
//import com.indexingsystem.system.service.SchedulerManageService;
//
//@Service
//public class SchedulerManageServiceImpl implements SchedulerManageService {
//	private static Logger LogCvt = LoggerFactory.getLogger(SchedulerManageServiceImpl.class);
//      
//	    @Autowired  
//	    private Scheduler scheduler;  
//	      
//	    /** 
//	     * 新增任务 
//	     * @param scheduleJob 
//	     */  
//	    @Override  
//	    public void add(Tasks tasks)  throws Exception{  
//	        if(StringUtils.isNotBlank(tasks.getCronExpress())){  
//	            this.addJobCron(tasks);  
//	        }else{  
//	            this.addJobSimple(tasks);  
//	        }  
//	    }  
//	      
//	    /** 
//	     * 更新任务 
//	     * @param scheduleJob 
//	     */  
//	    @Override  
//	    public void update(Tasks tasks)  throws Exception{  
//	        if(!StringUtils.isNotBlank(tasks.getCronExpress())){  
//	            this.updateJobCron(tasks);  
//	        }else{  
//	            this.updateJobSimple(tasks);  
//	        }  
//	    }  
//	      
//	    /** 
//	     * 新增任务 
//	     * @param scheduleJob 
//	     * @throws Exception  
//	     */  
//	    @SuppressWarnings("unchecked")  
//	    @Override  
//	    public void addJobCron(Tasks tasks) throws Exception{  
//	        TriggerKey triggerKey = TriggerKey.triggerKey(tasks.getTaskName(), Scheduler.DEFAULT_GROUP);  
//	        //任务触发  
//	        Trigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);  
//	        if (null == trigger) {  
//	            JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(tasks.getAssemblyName()))  
//	              .withIdentity(tasks.getTaskName(), Scheduler.DEFAULT_GROUP).build();  
//	            jobDetail.getJobDataMap().put("scheduleJob", tasks);  
//	              
//	            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(tasks.getCronExpress());  
//	            /*withMisfireHandlingInstructionDoNothing 
//	            ——不触发立即执行 
//	            ——等待下次Cron触发频率到达时刻开始按照Cron频率依次执行 
//	 
//	            withMisfireHandlingInstructionIgnoreMisfires 
//	            ——以错过的第一个频率时间立刻开始执行 
//	            ——重做错过的所有频率周期后 
//	            ——当下一次触发频率发生时间大于当前时间后，再按照正常的Cron频率依次执行 
//	 
//	            withMisfireHandlingInstructionFireAndProceed 
//	            ——以当前时间为触发频率立刻触发一次执行 
//	            ——然后按照Cron频率依次执行*/  
//	            trigger = TriggerBuilder.newTrigger().withIdentity(tasks.getTaskName(), Scheduler.DEFAULT_GROUP).withSchedule(cronScheduleBuilder.withMisfireHandlingInstructionDoNothing()).build();  
//	            scheduler.scheduleJob(jobDetail, trigger);  
////	            log.info(CC.LOG_PREFIX + "新增Cron任务："+JasonUtils.Object2String(scheduleJob));  
//	        }else {  
//	            // Trigger已存在，那么更新相应的定时设置  
//	            //表达式调度构建器  
//	            /*CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); 
//	            //按新的cronExpression表达式重新构建trigger 
//	            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder.withMisfireHandlingInstructionDoNothing()).build(); 
//	            JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 
//	            JobDetail jobDetail = scheduler.getJobDetail(jobKey); 
//	            jobDetail.getJobDataMap().put("scheduleJob", scheduleJob); 
//	            //按新的trigger重新设置job执行 
//	            scheduler.rescheduleJob(triggerKey, trigger); 
//	            log.info(CC.LOG_PREFIX + "任务"+JasonUtils.Object2String(scheduleJob)+"已经存在，更新trigger");*/  
//	              
//	            this.updateJobCron(tasks);  
//	        }  
//	    }  
//	      
//	    /** 
//	     * 更新任务的时间表达式 
//	     * @param scheduleJob 
//	     */  
//	    @Override  
//	    public void updateJobCron(Tasks tasks) throws Exception{  
//	        /*TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 
//	 
//	        //获取trigger，即在spring配置文件中定义的 bean id="myTrigger" 
//	        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); 
//	 
//	        //表达式调度构建器 
//	        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); 
//	 
//	        //按新的cronExpression表达式重新构建trigger 
//	        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder.withMisfireHandlingInstructionDoNothing()).build(); 
//	        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 
//	        JobDetail jobDetail = scheduler.getJobDetail(jobKey); 
//	        jobDetail.getJobDataMap().put("scheduleJob", scheduleJob); 
//	        //按新的trigger重新设置job执行 
//	        scheduler.rescheduleJob(triggerKey, trigger);*/  
//	          
//	          
//	        //为什么要删除再新增呢？因为不这样，JobDetail的JobDataMap不更新。注解什么都试过了，没起作用。  
//	        this.deleteJob(tasks);  
//	        this.addJobCron(tasks);  
////	        log.info(CC.LOG_PREFIX + "更新Cron任务（先删除再更新）："+JasonUtils.Object2String(scheduleJob));  
//	    }  
//	      
//	    /** 
//	     * 新增任务 
//	     * @param scheduleJob 
//	     * @throws Exception  
//	     */  
//	    @Override  
//	    @SuppressWarnings("unchecked")  
//	    public void addJobSimple(Tasks tasks) throws Exception{  
//	        TriggerKey triggerKey = TriggerKey.triggerKey(tasks.getTaskName(), Scheduler.DEFAULT_GROUP);  
//	        //任务触发  
//	        Trigger trigger = (Trigger) scheduler.getTrigger(triggerKey);  
//	        if (null == trigger) {  
//	            JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(tasks.getAssemblyName()))  
//	              .withIdentity(tasks.getTaskName(), Scheduler.DEFAULT_GROUP).build();  
//	            jobDetail.getJobDataMap().put("scheduleJob", tasks);  	              
////	            SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule(); 	            
//	            trigger = TriggerBuilder.newTrigger().withIdentity(tasks.getTaskName(), Scheduler.DEFAULT_GROUP)
//                        .startNow().withSchedule(CronScheduleBuilder.cronSchedule(tasks.getCronExpress())).build();
//                
//                scheduler.scheduleJob(jobDetail,trigger);
//                scheduler.start();
////	            Date triggerStartTime = new Date();  
////	            if("秒".equals(scheduleJob.getTimeType()) ||   
////	                    "second".equalsIgnoreCase(scheduleJob.getTimeType())){  
////	                simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue());  
////	                triggerStartTime = DateUtils.dateAddSeconds(triggerStartTime, scheduleJob.getTimeValue());  
////	            }else if("分".equals(scheduleJob.getTimeType()) || "分钟".equals(scheduleJob.getTimeType()) ||   
////	                    "minute".equalsIgnoreCase(scheduleJob.getTimeType())){  
////	                simpleScheduleBuilder.withIntervalInMinutes(scheduleJob.getTimeValue());  
////	                triggerStartTime = DateUtils.dateAddMinutes(triggerStartTime, scheduleJob.getTimeValue());  
////	            }else if("时".equals(scheduleJob.getTimeType()) || "小时".equals(scheduleJob.getTimeType()) ||   
////	                    "hour".equalsIgnoreCase(scheduleJob.getTimeType())){  
////	                simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue());  
////	                triggerStartTime = DateUtils.dateAddHours(triggerStartTime, scheduleJob.getTimeValue());  
////	            }else if("天".equals(scheduleJob.getTimeType()) ||   
////	                    "date".equalsIgnoreCase(scheduleJob.getTimeType())){  
////	                simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()*24);//2017-09-15修正问题，少了24  
////	                triggerStartTime = DateUtils.dateAddDays(triggerStartTime, scheduleJob.getTimeValue());  
////	            }  
//	              
////	            trigger = TriggerBuilder.newTrigger().withIdentity(tasks.getTaskName(), Scheduler.DEFAULT_GROUP)  
////	                    .startAt(tasks.getCronExpress()).withSchedule(simpleScheduleBuilder.repeatForever().withMisfireHandlingInstructionNextWithRemainingCount()).build();  
//	            scheduler.scheduleJob(jobDetail, trigger);  
////	            log.info(CC.LOG_PREFIX + "新增简单任务："+JasonUtils.Object2String(scheduleJob));  
//	        }else {  
//	            this.updateJobCron(tasks);  
//	        }  
//	    }  
//	      
//	    /** 
//	     * 更新任务的时间表达式 
//	     * @param scheduleJob 
//	     * @throws Exception  
//	     */  
//	    @Override  
//	    public void updateJobSimple(Tasks tasks) throws Exception{  
//	        //为什么要删除再新增呢？因为不这样，JobDetail的JobDataMap不更新。注解什么都试过了，没起作用。  
//	        this.deleteJob(tasks);  
//	        this.addJobSimple(tasks);  
////	        log.info(CC.LOG_PREFIX + "更新简单任务（先删除再更新）："+JasonUtils.Object2String(scheduleJob));  
//	    }  
//	      
//	      
//	      
//	    /** 
//	     * 暂停任务 
//	     * @param scheduleJob 
//	     * @throws Exception  
//	     */  
//	    @Override  
//	    public void pauseJob(Tasks tasks) throws Exception{  
//	        JobKey jobKey = JobKey.jobKey(tasks.getTaskName(), Scheduler.DEFAULT_GROUP);  
//	        scheduler.pauseJob(jobKey);  
////	        log.info(CC.LOG_PREFIX + "暂停任务："+JasonUtils.Object2String(scheduleJob));  
//	    }  
//	      
//	    /** 
//	     * 暂停全部任务 
//	     * @throws SchedulerException  
//	     */  
//	    @Override  
//	    public void pauseAll() throws Exception{  
//	        scheduler.pauseAll();  
////	        log.info(CC.LOG_PREFIX + "暂停所有任务");  
//	    }  
//	      
//	    /** 
//	     * 恢复任务 
//	     * @param scheduleJob 
//	     * @throws Exception  
//	     */  
//	    @Override  
//	    public void resumeJob(Tasks tasks) throws Exception{  
//	        JobKey jobKey = JobKey.jobKey(tasks.getTaskName(), Scheduler.DEFAULT_GROUP);  
//	        scheduler.resumeJob(jobKey);  
////	        log.info(CC.LOG_PREFIX + "恢复任务："+JasonUtils.Object2String(scheduleJob));  
//	    }  
//	      
//	      
//	    /** 
//	     * 恢复所有任务 
//	     * @throws Exception  
//	     */  
//	    @Override  
//	    public void resumeAll() throws Exception{  
//	        scheduler.resumeAll();  
////	        log.info(CC.LOG_PREFIX + "恢复所有任务");  
//	    }  
//	      
//	    /** 
//	     * 删除任务后，所对应的trigger也将被删除 
//	     * @param scheduleJob 
//	     * @throws Exception  
//	     */  
//	    @Override  
//	    public void deleteJob(Tasks tasks) throws Exception{  
//	        JobKey jobKey = JobKey.jobKey(tasks.getTaskName(), Scheduler.DEFAULT_GROUP);  
//	        scheduler.pauseJob(jobKey);//先暂停任务  
//	        scheduler.deleteJob(jobKey);//再删除任务  
////	        log.info(CC.LOG_PREFIX + "删除任务："+JasonUtils.Object2String(scheduleJob));  
//	    }  
//	      
//	    /** 
//	     * 立即运行任务 
//	     * @param scheduleJob 
//	     * @throws Exception  
//	     */  
//	    @Override  
//	    public void triggerJob(Tasks tasks) throws Exception{  
//	        JobKey jobKey = JobKey.jobKey(tasks.getTaskName(), Scheduler.DEFAULT_GROUP);  
//	        scheduler.triggerJob(jobKey);  
////	        log.info(CC.LOG_PREFIX + "运行任务："+JasonUtils.Object2String(scheduleJob));  
//	    }  
//	      
//	      
//	    /** 
//	     * 获取quartz调度器的计划任务 
//	     * @return 
//	     */  
//	    @Override  
//	    public List<Tasks> getScheduleJobList(){  
//	        List<Tasks> jobList = null;  
//	        try {  
//	            GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();  
//	            Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);  
//	            jobList = new ArrayList<Tasks>();  
//	            for (JobKey jobKey : jobKeys) {  
//	                List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);  
//	                for (Trigger trigger : triggers) {  
//	                	Tasks job = new Tasks();  
//	                	job.setTaskName(jobKey.getName());	                	
////	                    job.setJobGroup(jobKey.getGroup());  
//	                	job.setAssemblyName(jobKey.getClass().toString());
//	                    job.setRemark("触发器:" + trigger.getKey());  
//	                    Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());  
//	                    job.setTaskStatus(Integer.parseInt(triggerState.name()));  
//	                    if (trigger instanceof CronTrigger) {  
//	                        CronTrigger cronTrigger = (CronTrigger) trigger;  
//	                        String cronExpression = cronTrigger.getCronExpression();  
//	                        job.setCronExpress(cronExpression);  
//	                    }
////	                    else if(trigger instanceof SimpleTrigger){  
////	                        SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;  
////	                        long milliseconds = simpleTrigger.getRepeatInterval();  
////	                        job.setTimeValue((int) (milliseconds/1000));  
////	                    }  
//	                    jobList.add(job);  
//	                }  
//	            }  
//	        } catch (Exception e) {  
//	            e.printStackTrace();  
//	        }  
//	        return jobList;  
//	    }  
//	      
//	    /** 
//	     * 获取quartz调度器的运行任务 
//	     * @return 
//	     */  
//	    @Override  
//	    public List<Tasks> getScheduleJobRunningList(){  
//	        List<Tasks> jobList = null;  
//	        try {  
//	            List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();  
//	            jobList = new ArrayList<Tasks>(executingJobs.size());  
//	            for (JobExecutionContext executingJob : executingJobs) {  
//	              Tasks job = new Tasks();  
//	              JobDetail jobDetail = executingJob.getJobDetail();  
//	              JobKey jobKey = jobDetail.getKey();  
//	              Trigger trigger = executingJob.getTrigger();  
//	              job.setTaskName(jobKey.getName());	                	
////                  job.setJobGroup(jobKey.getGroup());  
//              	  job.setAssemblyName(jobKey.getClass().toString());
//                  job.setRemark("触发器:" + trigger.getKey());  
//                  Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());  
//                  job.setTaskStatus(Integer.parseInt(triggerState.name())); 	              
//	              if (trigger instanceof CronTrigger) {  
//	                  CronTrigger cronTrigger = (CronTrigger) trigger;  
//	                  String cronExpression = cronTrigger.getCronExpression();  
//	                  job.setCronExpress(cronExpression);  
//	              }
////	              else if(trigger instanceof SimpleTrigger){  
////	                  SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;  
////	                  long milliseconds = simpleTrigger.getRepeatInterval();  
////	                  job.setTimeValue((int) (milliseconds/1000));  
////	              }  
//	              jobList.add(job);  
//	            }  
//	        } catch (Exception e) {  
//	            e.printStackTrace();  
//	        }  
//	        return jobList;  
//	    }  
//
//}
